package com.example.myesstudy.controller;

import com.example.myesstudy.pojo.EsProduct;
import com.example.myesstudy.service.SpringDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 搜索商品管理Controller 推荐使用 restHighLevelClient
 *
 *  springboot 2.3.3.RELEASE  对应 es 7.6.1 对应  spring data elasticsearch 4.0.3
 *
 */
@Controller
@RequestMapping("/esProduct")
public class SpringDataController {

    @Autowired
    private SpringDataService springDataService;

    // http://127.0.0.1:9990/esProduct/create/1
    @RequestMapping(value = "/create/{id}", method = RequestMethod.GET)
    @ResponseBody
    public String create(@PathVariable Long id) {
        EsProduct esProduct = springDataService.create(id);

        return "";
    }


    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    @ResponseBody
    public String delete(@PathVariable Long id) {
        springDataService.delete(id);
        return "";
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @RequestMapping(value = "/delete/batch", method = RequestMethod.GET)
    @ResponseBody
    public String delete(@RequestParam("ids") List<Long> ids) {
        springDataService.delete(ids);
        return "";
    }


    /**
     * 简单搜索
     *
     * @param keyword
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/search/simple", method = RequestMethod.GET)
    @ResponseBody
    public String search(@RequestParam(required = false) String keyword,
                         @RequestParam(required = false, defaultValue = "0") Integer pageNum,
                         @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        Page<EsProduct> esProductPage = springDataService.search(keyword, pageNum, pageSize);
        System.out.println("esProductPage.toString():" + esProductPage.toString() + " esProductPage.getContent():" + esProductPage.getContent().size());
        return esProductPage.getContent().toString();
    }

}
